package com.yt.al.service;

import io.appium.java_client.AppiumDriver;

import org.openqa.selenium.WebElement;

import com.yt.al.config.SWGJDriver;
import com.yt.al.object.SWGJObject;
import com.yt.al.util.TestUtil;
import com.yt.al.util.WriteLogUtil;

public class SWGJService extends BaseService {

	AppiumDriver driver = null;
	public AppiumDriver getDriver() {
		return driver;
	}


	public void setDriver(AppiumDriver driver) {
		this.driver = driver;
	}

	SWGJObject swgjObj = null;

	public SWGJService(String url, String udid) {
		driver = SWGJDriver.returnDriver(url, udid);
		swgjObj = new SWGJObject(driver);
		init(driver);
	}
	

	public int firstLogin(String phone, String pwd)
	{
		int rel = 1;
		try
		{
			TestUtil.waits(2000);
			WriteLogUtil.writeMsgToFile("点击立即体验元素");
			clickDeal(swgjObj.getComeIn());
			

			TestUtil.waits(2000);
			WriteLogUtil.writeMsgToFile("点击屏幕");
			press(200, 50);
			
			TestUtil.waits(1000);
			WriteLogUtil.writeMsgToFile("获取 右上角菜单 按钮，判断是否已经登录");
			WebElement topRight = swgjObj.getTopRight();
			if(topRight!=null)
			{
				WriteLogUtil.writeMsgToFile("点击右上角 菜单按钮");
				clickDeal(topRight);

				TestUtil.waits(1000);
				WriteLogUtil.writeMsgToFile("获取 消息设置 元素");
				WebElement alertSet = swgjObj.getAlertSet();
				if(alertSet==null)
				{
					WriteLogUtil.writeMsgToFile("系统不支持元素捕获，更换为屏幕点击");
					press(450, 116);
					
					TestUtil.waits(500);
					WriteLogUtil.writeMsgToFile("获取 开启状态栏， 判断是否点击成功");
					WebElement notify = swgjObj.getNotify();
					if(notify==null)
					{
						WriteLogUtil.writeMsgToFile("再次点击坐标 450,116");
						press(450, 116);
					}
				}
				else
				{
					WriteLogUtil.writeMsgToFile("点击 消息设置 按钮");
					clickDeal(alertSet);
				}
				
				
				TestUtil.waits(2000);
				WriteLogUtil.writeMsgToFile("滑动屏幕");
				verzlignFlickTimes(3);
				
				TestUtil.waits(500);
				WriteLogUtil.writeMsgToFile("点击  注销  按钮");
				clickDeal(swgjObj.getLoginout());
				
				TestUtil.waits(1000);
				WriteLogUtil.writeMsgToFile("点击  注销确定  按钮");
				clickDeal(swgjObj.getOutsure());
				
			}
						
			
			TestUtil.waits(3000);
			WriteLogUtil.writeMsgToFile("点击 服务密码 按钮");
			clickDeal(swgjObj.getServicePass());
			
			WriteLogUtil.writeMsgToFile("输入手机号码");
			writeDeal(swgjObj.getPhone(), phone);
			
			WriteLogUtil.writeMsgToFile("输入手机密码");
			writeDeal(swgjObj.getPwd(), pwd);
			
			WriteLogUtil.writeMsgToFile("点击 登陆 按钮");
			clickDeal(swgjObj.getLogin());
			
			checkIsLogining();
			
			WebElement loginBtn = swgjObj.getLogin();
			WriteLogUtil.writeMsgToFile("判断是否登录成功");
			if(loginBtn==null)
			{
				rel = 1;
			}
			else
			{
				WriteLogUtil.writeMsgToFile("第二次点击 登陆 按钮");
				clickDeal(swgjObj.getLogin());
				checkIsLogining();
				
				loginBtn = swgjObj.getLogin();
				WriteLogUtil.writeMsgToFile("判断是否登录成功");
				if(loginBtn!=null)
				{
					rel = 2;
				}
			}

		}
		catch(Exception e)
		{
			WriteLogUtil.writeMsgToFile("操作中捕获到异常");
			rel = 0;
		}
		
		return rel;
	}
	
	private void checkIsLogining()
	{
		try
		{
			while(swgjObj.getLogin()!=null)
			{
				if(swgjObj.getLogin().getText().indexOf("请稍")>0)
				{
					WriteLogUtil.writeMsgToFile("正在网络请求，等待1秒钟");
					TestUtil.waits(1000);
				}
				else
				{
					break;
				}
			}	
		}
		catch(Exception e)
		{
			
		}
		
	}
	
	public int firstAgainLogin(String phone, String pwd)
	{
		int rel = 1;
		try
		{
			WriteLogUtil.writeMsgToFile("输入手机号码");
			writeDeal(swgjObj.getPhone(), phone);
			
			WriteLogUtil.writeMsgToFile("输入手机密码");
			writeDeal(swgjObj.getPwd(), pwd);
			
			WriteLogUtil.writeMsgToFile("点击 登陆 按钮");
			clickDeal(swgjObj.getLogin());
			checkIsLogining();
			
			WebElement loginBtn = swgjObj.getLogin();
			WriteLogUtil.writeMsgToFile("判断是否登录成功");
			if(loginBtn==null)
			{
				rel = 1;
			}
			else
			{
				WriteLogUtil.writeMsgToFile("第二次点击 登陆 按钮");
				clickDeal(swgjObj.getLogin());
				checkIsLogining();
				
				loginBtn = swgjObj.getLogin();
				WriteLogUtil.writeMsgToFile("判断是否登录成功");
				if(loginBtn!=null)
				{
					rel = 2;
				}
			}

		}
		catch(Exception e)
		{
			rel = 0;
		}
		
		return rel;
	}
	
	public int secondLogin(String phone, String pwd)
	{
		int rel = 1;
		try
		{
			WriteLogUtil.writeMsgToFile("是否弹出流量提醒框");
			WebElement closeBtn = swgjObj.getGprsAlertCloseButton();
			if(closeBtn!=null)
			{
				clickDeal(closeBtn);
			}
			
			WriteLogUtil.writeMsgToFile("点击屏幕");
			swgjObj.tapScreen(15, 15);
			
			TestUtil.waits(500);
			WriteLogUtil.writeMsgToFile("点击 右上角菜单 按钮");
			clickDeal(swgjObj.getTopRight());
			
			TestUtil.waits(500);
			WriteLogUtil.writeMsgToFile("获取 消息设置 元素");
			WebElement alertSet = swgjObj.getAlertSet();
			if(alertSet==null)
			{
				WriteLogUtil.writeMsgToFile("系统不支持元素捕获，更换为屏幕点击");
				press(450, 116);
				
				TestUtil.waits(500);
				WriteLogUtil.writeMsgToFile("获取 开启状态栏， 判断是否点击成功");
				WebElement notify = swgjObj.getNotify();
				if(notify==null)
				{
					WriteLogUtil.writeMsgToFile("再次点击坐标 450,116");
					press(450, 116);
				}
			}
			else
			{
				WriteLogUtil.writeMsgToFile("点击 消息设置 按钮");
				clickDeal(alertSet);
			}
			
			TestUtil.waits(2000);
			WriteLogUtil.writeMsgToFile("滑动屏幕");
			verzlignFlickTimes(3);
			
			TestUtil.waits(500);
			WriteLogUtil.writeMsgToFile("点击  注销  按钮");
			clickDeal(swgjObj.getLoginout());
			
			TestUtil.waits(500);
			WriteLogUtil.writeMsgToFile("点击  注销确定  按钮");
			clickDeal(swgjObj.getOutsure());

			
			TestUtil.waits(200);
			WriteLogUtil.writeMsgToFile("点击 服务密码 按钮");
			clickDeal(swgjObj.getServicePass());
			
			WriteLogUtil.writeMsgToFile("输入手机号码");
			writeDeal(swgjObj.getPhone(), phone);
			
			WriteLogUtil.writeMsgToFile("输入手机密码");
			writeDeal(swgjObj.getPwd(), pwd);
			
			WriteLogUtil.writeMsgToFile("点击 登陆 按钮");
			clickDeal(swgjObj.getLogin());
			checkIsLogining();
			
			WebElement loginBtn = swgjObj.getLogin();
			WriteLogUtil.writeMsgToFile("判断是否登录成功");
			if(loginBtn==null)
			{
				rel = 1;
			}
			else
			{
				WriteLogUtil.writeMsgToFile("第二次点击 登陆 按钮");
				clickDeal(swgjObj.getLogin());
				checkIsLogining();
				
				loginBtn = swgjObj.getLogin();
				WriteLogUtil.writeMsgToFile("判断是否登录成功");
				if(loginBtn!=null)
				{
					rel = 2;
				}
			}			
			
			
		}
		catch(Exception e)
		{
			rel = 0;
		}
		
		return rel;
	}
}
